import java.util.*;
import java.io.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class code3_2 {
    public static void main(String[] args) throws Throwable{
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(reader.readLine());
        String[] s = reader.readLine().split(" ");
        String str1 = s[0];
        String str2 = s[1];
        String[] str = new String[n];
        for(int i=0;i<n;i++) {
            str[i] = reader.readLine();
        }
        int prev1 = -1;
        int prev2 = -1;
        int ret = 0x3f3f3f3f;
        for(int i=0;i<n;i++) {
            if(str[i].equals(str1)) prev1 = i;
            if(str[i].equals(str2)) prev2 = i;
            if(prev1!=-1&&prev2!=-1&&str[prev1].equals(str1)&&str[prev2].equals(str2))  ret = Math.min(ret,Math.abs(prev1-prev2));

        }

        System.out.print(ret == 0x3f3f3f3f?-1:ret);
    }
}